#pragma once 
#include<iostream>
#include "common.hpp"
//网络地址和主机地址转换的类

class inetaddr
{
    public:
    inetaddr(struct sockaddr_in& addr):_addr(addr)
    {
        //网络转主机
        _port = ntohs(addr.sin_port);
       // _ip = inet_ntoa(addr.sin_addr);
       char ipbuffer[64];
       inet_ntop(AF_INET,&_addr.sin_addr,ipbuffer,sizeof(_addr));;
       _ip = ipbuffer;
    }
    inetaddr(const std::string &ip,uint16_t port)
    :_ip(ip)
    ,_port(port)
    {
        //主机转网络
        memset(&_addr,0,sizeof(_addr));
        _addr.sin_family = AF_INET;
        inet_pton(AF_INET,_ip.c_str(),&_addr.sin_addr);
        _addr.sin_port =htons(_port);
    }

    //再次重载
     inetaddr(uint16_t port)
    :_port(port)
    {
        //主机转网络
        memset(&_addr,0,sizeof(_addr));
        _addr.sin_family = AF_INET;
        _addr.sin_addr.s_addr = INADDR_ANY;
        _addr.sin_port =htons(_port);
    }
    uint16_t Port()
    {
        return _port;
    }
    std::string Ip()
    {
        return _ip;
    }
    struct sockaddr_in& Netaddr()
    {
        return _addr;
    }
    const struct sockaddr* Netaddrptr()
    {
        return CONV(_addr);
    }
    socklen_t Netaddrlen()
    {
        return sizeof(_addr);
    }
    bool operator==(const inetaddr& addr)
    {
        return addr._ip ==_ip&& addr._port==_port;
    }
    std::string Stringaddr()//
    {
        return _ip+":"+std::to_string(_port);
    }
    ~inetaddr()
    {}
    private:
    struct sockaddr_in _addr;
    std::string _ip;
    uint16_t _port;
};